// 窗口相关的处理
import { reactive,ref,provide } from 'vue'
import { uuid } from 'vue-uuid'
import {useStore} from 'vuex'
export default function (){
    const store = useStore()
    let list = reactive([])
    let isSelected = ref('')

    let loading  = ref(true)
    let searchValue = reactive({})
    let currentPage = reactive({})

    let elW = 0;
    let elH = 0;
    let elLeft = 0;
    let elTop = 0;


    //窗口选中
    function handleSelect(item) {
        isSelected.value = item.cid
    }

    //iframe加载监听函数
    function iframeLoad() {
        loading.value=false 
    }

    // 窗口缩放
    function handleZoom(item) {
        isSelected.value = item.cid
        setTimeout(() => {
            let window = document.querySelector('.active')
            if(window.offsetWidth<document.body.offsetWidth||window.offsetHeight<document.body.offsetHeight-80) {
                //全屏展示
                elW = window.offsetWidth
                elH = window.offsetHeight
                elLeft = window.offsetLeft
                elTop = window.offsetTop
                window.style.width = '100%'
                window.style.height = '100%'
                window.style.left = '0%'
                window.style.top = '0%'
             
            }else{
                if(elH>0||elW>0||elLeft>0||elTop>0){
                    window.style.width = elW + 'px'
                    window.style.height = elH + 'px'
                    window.style.left = elLeft + 'px'
                    window.style.top = elTop + 'px'
                    return
                }
               
                window.style.width = document.body.offsetWidth-100 + 'px'
                window.style.height = document.body.offsetHeight-100 + 'px'
            }
        }, 30);
    }

    // 窗口隐藏
    function hideWindow(item) {
        list.forEach(value => {
            if(value.id === item.id){
                value.child.forEach(value1 => {
                    if(value1.cid === item.cid){
                        value1.isShow=false
                    }
                });
            }
        });
    }

    // 窗口显示
    function showWindow(item){
        list.forEach(value => {
            if(value.id === item.id){
                value.child.forEach(value1 => {
                    if(value1.cid === item.cid){
                        value1.isShow=!value1.isShow
                        if(value1.isShow){
                            isSelected.value=item.cid
                        }else{
                            isSelected.value=''
                        }
                        
                    }
                });
            }
        });
    }

    //窗口关闭
    function handleClose(item){
        list.forEach((value,index) => {
            if(value.id===item.id){
                value.child.forEach((value1,index1) => {
                    if(value1.cid === item.cid){
                        value.child.splice(index1,1)
                        if(value.child.length<=0){
                            list.splice(index,1)
                        }
                    }
                });
            }
        });
    }

    // 浏览器搜索
    function searchUrl(val,item){
        list.forEach((value,index) => {
            if(value.id===item.id){
                value.child.forEach((value1,index1) => {
                    if(value1.cid === item.cid){
                        value1.page.forEach(value2 => {
                            if(value2.pageId===currentPage[item.cid]){
                                if(val.indexOf('http://')>-1||val.indexOf('https://')>-1) {
                                    if(val.indexOf('https://bing.com/search?q=')>-1||val.indexOf('https://cn.bing.com/search?q=')>-1){
                                        value2.url=val
                                        searchValue[value2.pageId]=val
                                    }else{
                                        if(val.indexOf('https://bing.com/')>-1||val.indexOf('https://cn.bing.com/')>-1){
                                            let temp = val.indexOf('/',8)
                                            let url = val.slice(0,temp+1)
                                            let params = val.slice(temp+1)
                                            value2.url=`${url}search?q=${params}`
                                            searchValue[value2.pageId]=value2.url
                                        }else{
                                            value2.url=val
                                            searchValue[value2.pageId]=val
                                        }
                                    }
                                    
                                }else{
                                    let url = `https://bing.com/search?q=${val}`
                                    searchValue[value2.pageId]=url
                                    value2.url=url
                                 
                                }
                                // value2.history = []                                 
                                value2.history.push(value2.url)
                                value2.reload++
                                console.log(value2.history);
                            }
                        });
                    }
                });
            }
        });
    }
    
    // 添加浏览器tab页
    function handleAddTab(value){
        let page = {
            pageId:uuid.v4(),
            url:value.link,
            title:value.name,
            icon:value.icon,
            cid:value.cid,
            reload:0,
            history:[
                value.link
            ]
        }
        list.forEach(item => {
            if(item.id===value.id){
                item.child.forEach(item1 => {
                    if(item1.cid===value.cid){
                        item1.page.push(page)
                    }
                });
            }
        });
        searchValue[page.pageId] = page.url
        currentPage[value.cid] = page.pageId
    }

    // 切换浏览器tab页面
    function handleChangeTab(value){
        currentPage[value.cid] = value.pageId
    }

    // 关闭浏览器tab页面
    function handleCloseTab(val1,val2){
        list.forEach((item,index) => {
            if(item.id===val1.id){
                item.child.forEach((item1,index1) => {
                    if(item1.cid===val2.cid){
                        item1.page.forEach((item2,index2) => {
                            if(item2.pageId===val2.pageId){
                                item1.page.splice(index2,1)
                                if(item1.page.length<=0){
                                    item.child.splice(index1,1)
                                    if(item.child.length<=0){
                                        list.splice(index,1)
                                    }
                                }else{
                                    if(index2===0){
                                        handleChangeTab(item1.page[index2 + 1])
                                    }else{
                                        console.log(currentPage.value);
                                        handleChangeTab(item1.page[index2 - 1])
                                    }
                                }
                            }
                        });
                    }
                });
            }
        });
    }

    // 处理浏览器页面历史
    function handlePageHistory(val,app_sub,app_page){
        if(val==='back'){
            list.forEach(item1 => {
                if(item1.id===app_sub.id){
                    item1.child.forEach(item2 => {
                        if(item2.cid===app_sub.cid){
                            item2.page.forEach(item3 => {
                                if(item3.pageId===app_page.pageId){
                                    if(item3.reload>=1){
                                        item3.reload--
                                        item3.url=item3.history[item3.reload]
                                        searchValue[item3.pageId]=item3.history[item3.reload]
                                    }
                                }
                            });
                        }
                    });
                }
            });
        }else if(val==='go'){
            list.forEach(item1 => {
                if(item1.id===app_sub.id){
                    item1.child.forEach(item2 => {
                        if(item2.cid===app_sub.cid){
                            item2.page.forEach(item3 => {
                                if(item3.pageId===app_page.pageId){
                                    if(item3.reload<item3.history.length-1){
                                        item3.reload++
                                        item3.url=item3.history[item3.reload]
                                        searchValue[item3.pageId]=item3.history[item3.reload]
                                    }
                                }
                            });
                        }
                    });
                }
            });
        }else{}
    }

    // 浏览器刷新
    function handleRefresh(app_sub,app_page){
        list.forEach(item1 => {
            if(item1.id===app_sub.id){
                item1.child.forEach(item2 => {
                    if(item2.cid===app_sub.cid){
                        item2.page.forEach(item3 => {
                            if(item3.pageId===app_page.pageId){
                                item3.url=''
                                loading.value=true
                                setTimeout(() => {
                                    item3.url = searchValue[item3.pageId]
                                }, 100);
                                
                            }
                        });
                    }
                });
            }
        });
    }

    // 打开窗口
    function openWindow(app){
        if(app.type==='folder'){
            store.dispatch('handleFileNav',app)
            return true
        }
        if(app.isOutOpen===true){
            return window.open(app.link,'_blank')
        }
        let app_sub = {
            cid:uuid.v1(),
            id:app.id,
            pid:app.pid,
            name:app.name,
            icon:app.icon,
            isShow:true,
            link:app.link
        }
        let page = {
            pageId:uuid.v4(),
            url:app.link,
            title:app.name,
            icon:app.icon,
            cid:app_sub.cid,
            reload:0,
            history:[
                app.link
            ]
        }
        isSelected.value = app_sub.cid
        let flag = list.some((item)=>{
            return item.id===app.id
        })
        if(flag){
            list.forEach(item => {
                if(item.id===app.id&&item.isMore===true){
                    if(app.name==='Edge'){
                        app_sub.page=[]
                        app_sub.page.push(page)
                        searchValue[page.pageId] = page.url
                        currentPage[page.cid] = page.pageId
                    }
                    item.child.push(app_sub)
                }else{
                    //不允许重复打开
                }
            });
        }else{
            if(app.name==='Edge'){
                app_sub.page=[]
                app_sub.page.push(page)
                searchValue[page.pageId] = page.url
                currentPage[page.cid] = page.pageId
            }
            app.child.push(app_sub)
            list.push(app)
        }
    }

    provide('openWindow',openWindow)

    return {
        list,
        isSelected,
        loading,
        searchValue,
        currentPage,

        openWindow,
        iframeLoad,
        handleSelect,
        handleZoom,
        hideWindow,
        showWindow,
        handleClose,
        searchUrl,
        handleAddTab,
        handleChangeTab,
        handleCloseTab,
        handlePageHistory,
        handleRefresh,
    }

}